package com.hxs.email.util;

import jakarta.mail.Message;
import jakarta.mail.internet.InternetAddress;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.MailException;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessagePreparator;
import org.springframework.stereotype.Component;


@Component
public class EmailSender {

	@Autowired
	private JavaMailSender mailSender;

	@Value("${spring.mail.username}")
	private String sender;
	public void sendEmail(String receiver,String text) {



		MimeMessagePreparator preparator = mimeMessage -> {
            mimeMessage.setRecipient(Message.RecipientType.TO,
                    new InternetAddress(receiver));
            mimeMessage.setFrom(new InternetAddress(sender));
            mimeMessage.setText(text);
        };

		try {
			this.mailSender.send(preparator);
		}
		catch (MailException ex) {
			System.err.println(ex.getMessage());
		}
	}

}